package com.tencent.qplus.paddata.stat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.tencent.qplus.paddata.util.ConfigUtil;
//yunyang 1月4日修改，统计 12.20——1.4号下载数据
/**
 * 
 * 统计用户下载的源信息
 * @author shiangzhang
 *
 * 2012-1-4
 */
public class MarketStat extends Job {

    private static String logFilePath = ConfigUtil.getString("logfile_path");
    private static Pattern logLinePattern = Pattern
            .compile("^\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}\\]");

    private Logger logger = Logger.getLogger(ParseLogFileJob.class);

    private File[] getLogFiles(final String statDate) {
        File path = new File(logFilePath);
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    String fileName = pathname.getName();
                    if (fileName.startsWith("msg-")
                            && fileName.endsWith(".log." + statDate))
                        return true;
                }
                return false;
            }
        };
        return path.listFiles(filter);
    }

    private File[] getLogFiles(final String statDate, String logFilePath) {
        File path = new File(logFilePath);
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    String fileName = pathname.getName();
                    if (fileName.startsWith("msg-")
                            && fileName.endsWith(".log." + statDate))
                        return true;
                }
                return false;
            }
        };
        return path.listFiles(filter);
    }

    HashMap<String, Integer> marketLoginCountMap = new HashMap<String, Integer>();
    HashMap<String, Integer> marketCountMap = new HashMap<String, Integer>();

    private void parseFile(String statDate, File logFile) {
        String tempLine = null;
        try {
            BufferedReader br = new BufferedReader(new FileReader(logFile));
            String line = null;

            int nextLineNum = 0;
            while ((line = br.readLine()) != null) {
                // try{
                if (line.indexOf("version=") > 0
                        && line.indexOf("version=18") != -1) {
                    tempLine = line;
                    nextLineNum = 0;
                } else {
                    nextLineNum++;
                    if (logLinePattern.matcher(line).find()) {
                        continue;
                    } else {
                        if (nextLineNum != 1)
                            continue;
                        tempLine += line;
                    }
                }
                tempLine = tempLine.replaceAll("'", "\\\\'");

                int index0 = tempLine.indexOf("DOWNLOADMARKET");// 登录上报
                // int index1 = tempLine.indexOf("downLoadMarket");// 设备上报
                if (index0 != -1) {
                    int lastDivIndex = tempLine.lastIndexOf("|");
                    String marketStr = tempLine.substring(lastDivIndex + 1);
                    String[] array = tempLine.split("\\|", 3);
                    if (array.length < 3)
                        continue;
                    String uin = array[1];
                    String tempStr = marketStr + "|" + uin;
                    if (!marketLoginCountMap.containsKey(tempStr)) {
                        marketLoginCountMap.put(tempStr, 1);
                    }
                }
                // } else if (index1 != -1) {
                // int lastDivIndex = tempLine.lastIndexOf("|");
                // String marketStr = tempLine.substring(lastDivIndex + 1);
                // if (marketCountMap.containsKey(marketStr)) {
                // int i = marketCountMap.get(marketStr);
                // marketCountMap.put(marketStr, ++i);
                // } else {
                // marketCountMap.put(marketStr, 1);
                // }
                // }
            }

        } catch (FileNotFoundException e) {
            logger.error(e);
        } catch (IOException e) {
            logger.error(e);
        }
    }

    public void resultOutPut(String file) {
        try {
            File f = new File(file);
            if (!f.exists()) {
                f.createNewFile();
            }
            FileWriter writer = new FileWriter(file);
            for (Iterator<String> it = marketCountMap.keySet().iterator(); it
                    .hasNext();) {
                String key = it.next();
                int value = marketCountMap.get(key);
                writer.write(key + "\t" + value + "\r\n");
            }
            writer.write("\r\n\r\n\r\n");
            Set<Entry<String, Integer>> set = marketLoginCountMap.entrySet();
            HashMap<String, Integer> countMap = new HashMap<String, Integer>();
            for (Iterator<Entry<String, Integer>> it = set.iterator(); it
                    .hasNext();) {
                Entry<String, Integer> entry = it.next();
                String tempStr = entry.getKey();
                String marketStr = tempStr.split("\\|")[0];
                if (countMap.containsKey(marketStr)) {
                    countMap.put(marketStr, countMap.get(marketStr) + 1);
                } else {
                    countMap.put(marketStr, 1);
                }
            }

            for (Iterator<String> it = countMap.keySet().iterator(); it
                    .hasNext();) {
                String key = it.next();
                int value = countMap.get(key);
                writer.write(key + "\t" + value + "\r\n");
            }

            writer.close();
        } catch (Exception e) {
            logger.error(e);
        }
    }

    @Override
    public void run() {
        String path = ConfigUtil.getString("logfile_path");
        File[] logFiles = getLogFiles(statDate, path+ "164");
        for (File logFile : logFiles) {
            System.out.println(logFile.getName());
            parseFile(statDate, logFile);
        }
        logFiles = getLogFiles(statDate, path + "213");
        for (File logFile : logFiles) {
            parseFile(statDate, logFile);
        }
        resultOutPut("/data/java/shiang/paddatastat/maket.txt");
        // resultOutPut("d:/maket.txt");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        MarketStat job = new MarketStat();
        job.stat(args);
    }

    @Override
    public void stat(String[] args) {
        String startDate="2011-11-24";
        for (int i = 0; i < 11; i++) {
            statDate = dateUtils.getDateStrByDaysFromDate(startDate,i);
            logger.info("start stat..................: " + statDate);
            run();
            logger.info("stat done..................: " + statDate);
        }
    }

    // TODO 需要检测的版本号
//    public static final String versionStr = "9";

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }
}
